﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.CustomHelpers;
using KMF.WebUI.Areas.KMFadmin.Models;
using KMF.WebUI.Areas.KMFadmin.Models.DiscountRequirement;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminDiscountController : Controller
    {
        //
        // GET: /Discount/

        public ActionResult DiscountList(int page = 1)
        {
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);
            var totalPage = 0;
            var totalRecord = 0;
            var model = DiscountDb.GetAllDiscount(page, itemsPerPage, ref totalPage, ref totalRecord) ?? new List<ViewDiscountPagingDto>();

            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
            ViewBag.TotalRecord = totalRecord;
            return View(model);
        }

        public ActionResult AddDiscount()
        {
            ViewBag.DiscountTypeList = DiscountTypeDb.GetAll() ?? new List<DiscountTypeDto>();
            ViewBag.DiscountLimitationList = DiscountLimitationDb.GetAll() ?? new List<DiscountLimitationDto>();
            return View();
        }

        [HttpPost]
        public ActionResult AddDiscount(DiscountDto model)
        {
            model.DiscountAmount = model.DiscountAmount < 0 ? 0 : model.DiscountAmount;
            model.DiscountPercentage = model.DiscountPercentage < 0 ? 0 : model.DiscountPercentage;
            var result = DiscountDb.AddDiscount(model);
            return RedirectToAction("DiscountList", "AdminDiscount");
        }

        public ActionResult EditDiscount(int id)
        {
            ViewBag.DiscountTypeList = DiscountTypeDb.GetAll() ?? new List<DiscountTypeDto>();
            ViewBag.DiscountLimitationList = DiscountLimitationDb.GetAll() ?? new List<DiscountLimitationDto>();
            
            var model = DiscountDb.GetById(id) ?? new DiscountDto();
            return View(model);
        }

        [HttpPost]
        public ActionResult EditDiscount(DiscountDto model)
        {
            model.DiscountAmount = model.DiscountAmount < 0 ? 0 : model.DiscountAmount;
            model.DiscountPercentage = model.DiscountPercentage < 0 ? 0 : model.DiscountPercentage;
            DiscountDb.Update(model);
            return RedirectToAction("DiscountList", "AdminDiscount");
        }
        
        public JsonResult DeleteDiscount(int id)
        {
            var result = DiscountDb.DeleteDiscount(id);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }

        #region Discount Requirement has spent amount product
        
        public PartialViewResult DiscountRequirement(int id)
        {
            ViewBag.DiscountRequirementList = CommonHelpers.CreateListHasDefaultName(DiscountRequirementTypeDb.GetAll() ?? new List<DiscountRequirementTypeDto>(), "Id", "Name", "0", "Chọn điều kiện để được chiết khấu");
            ViewBag.DiscountId = id;
            return PartialView();
        }

        public ActionResult AjaxHtmlInputAmountProduct(int discountId, string systemRuleName)
        {
            ViewBag.SystemRuleName = systemRuleName;
            ViewBag.DiscountId = discountId;
            return PartialView();
        }

        [HttpPost]
        public JsonResult AjaxHtmlInputAmountProduct(HasSpentAmount model, int discountId)
        {
            var discountRequirementList = DiscountRequirementDb.GetByDiscountId(discountId) ;
            if(discountRequirementList != null)
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
            var resultAddDiscount = DiscountDb.AddDiscountRequirement(new DiscountRequirementDto
                                                      {
                                                          DiscountId = discountId,
                                                          DiscountRequirementRuleSystemName = model.DiscountRequirementRuleSystemName
                                                      });
            var resultAddSetting = SettingDb.AddSetting(new SettingDto
                                         {
                                             Name = model.DiscountRequirementRuleSystemName + "-" + resultAddDiscount,
                                             Value = model.Quantity
                                         });
            return Json(resultAddDiscount > 0 & resultAddSetting > 0, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AjaxHtmlEditAmountProduct(int discountId)
        {
            var discountRequirement = DiscountRequirementDb.GetByDiscountId(discountId) ?? new DiscountRequirementDto();
            var settingName = discountRequirement.DiscountRequirementRuleSystemName + "-" + discountRequirement.Id;

            ViewBag.DiscountId = discountId;
            var setting = SettingDb.GetByName(settingName) ?? new SettingDto();
            return PartialView(setting);
        }

        [HttpPost]
        public JsonResult AjaxHtmlEditAmountProduct(SettingDto model)
        {
            var result = SettingDb.Update(model);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteDiscountRequirement(int discountId)
        {
            var result = DiscountRequirementDb.DeleteDiscountRequirement(discountId);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }

        #endregion 

    }
}
